<template>
  <div>
    <div>
      <div class="handle-box">
        <el-date-picker
          v-model="timeArray"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="margin-right: 10px"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
        <el-button type="primary" icon="el-icon-search" @click="getTimeLinePage()">搜索</el-button>
        <el-button type="danger" @click="clearSearch()">清除参数</el-button>
        <el-button type="primary" @click="addTimeLine()">新增时间线</el-button>
      </div>

      <el-table
        :data="timeLines"
        border
        class="table"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="occurrenceTime"
          label="时间"
          align="center"
          width="300px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="内容"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
          width="300px"
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="修改时间"
          align="center"
          width="300px"
        ></el-table-column>
        <el-table-column label="操作" width="270" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="pagination.current"
          :page-size="pagination.size"
          :total="pagination.total"
          @current-change="handlePageChange"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog
      :title="title"
      :visible.sync="editVisible"
      width="30%"
      :before-close="handleClose"
      :append-to-body="true"
      destroy-on-close
      center
    >
      <div>
        <el-form ref="form" label-width="150px" :rules="rules">
          <el-form-item label="时间" prop="occurrenceTime">
            <el-date-picker
              v-model="form.occurrenceTime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input type="textarea" v-model="form.content"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timeArray: [],
      title: "",
      editVisible: false,
      pagination: {
        current: 1,
        size: 10,
        total: 0,
        startTime: null,
        endTime: null,
      },
      form: {
        id: null,
        occurrenceTime: null,
        content: null
      },
      timeLines: [],
      rules: {
        occurrenceTime: [
          {required: true, message: "时间不能为空", trigger: "change"},
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "change"},
        ],
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    };
  },

  computed: {},

  watch: {},

  created() {
    this.getTimeLinePage();
  },

  mounted() {
  },

  methods: {
    clearSearch() {
      this.timeArray = [];
      this.pagination = {
        current: 1,
        size: 10,
        total: 0,
        startTime: null,
        endTime: null
      };
      this.getTimeLinePage();
    },
    getTimeLinePage() {
      if (!this.$common.isEmpty(this.timeArray)) {
        this.pagination.startTime = this.timeArray[0];
        this.pagination.endTime = this.timeArray[1];
      }
      this.$createHttp.timeLinePage(this.pagination).then((res) => {
        if (res.code === 200) {
          this.timeLines = res.data.records;
          this.pagination.total = res.data.total;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      })
    },
    addTimeLine() {
      this.title = "新增";
      this.editVisible = true;
    },
    save() {
      if (!this.form.id) {//新增
        this.$createHttp.addTimeLine(this.form).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "新增成功",
              type: "success",
            });
            this.getTimeLinePage()
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        }).catch((error) => {
          this.$message({
            message: error,
            type: "error",
          });
        })
      } else {//修改
        this.$createHttp.updateTimeLine(this.form).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
      }
      this.handleClose();
      setTimeout(() => {
        this.getTimeLinePage();
      });
    },
    handleEdit(id) {
      this.title = "编辑";
      this.$createHttp.timeLineInfo(id).then((res) => {
        if (res.code === 200) {
          this.form.id = res.data.id;
          this.form.occurrenceTime = res.data.occurrenceTime;
          this.form.content = res.data.content;
          this.editVisible = true;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$createHttp.deleteTimeLine(id).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            setTimeout(() => {
              this.getTimeLinePage();
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
    handlePageChange(val) {
      this.pagination.current = val;
      this.getTimeLinePage();
    },
    handleClose() {
      this.form = {
        id: null,
        occurrenceTime: null,
        content: null
      }
      this.editVisible = false;
    }
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.table {
  width: 100%;
  font-size: 14px;
}

.pagination {
  margin: 20px 0;
  text-align: right;
}
</style>
